// ==UserScript==
// @name           Vkontakte My Header
// @namespace      http://polkila.googlecode.com
// @author:        Васютинский Олег http://vasyutinskiy.ru
// @version:       9.04
// @include        http://vkontakte.ru*
// @include        http://www.vkontakte.ru*
// ==/UserScript==

(function(){
	if (!window.ge) window = unsafeWindow;
	if (!window.ge) return;

	window.readCookie = function(name){
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++){
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	};
	window.createCookie = function(name,value,days){
	  if (days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	  }
	  else expires = "";
	  document.cookie = name+"="+value+expires+"; path=/";
	};
	var lang = parseInt(window.readCookie('remixlang'));
	var _title_ = lang?'Type image url':'&#1059;&#1082;&#1072;&#1078;&#1080;&#1090;&#1077; &#1072;&#1076;&#1088;&#1077;&#1089; &#1082;&#1072;&#1088;&#1090;&#1080;&#1085;&#1082;&#1080;';
	var _cancel_ = lang?'Cancel':'&#1054;&#1090;&#1084;&#1077;&#1085;&#1072;';
	var _example_ = lang?'For example':'&#1053;&#1072;&#1087;&#1088;&#1080;&#1084;&#1077;&#1088;';
	var _default_ = 'http://files.vasyutinskiy.ru/lj/xhead2_sh.gif';
	var u = window.readCookie('xhead');
	if (u==null) u = _default_;
	window.showHSDialog = function(){
		if (!window.ge('boxFader')) window.ge('content').innerHTML+='<div id="boxFader"><div id="boxWrap"><div id="boxBody"><div id="nameLabel"><div id="boxTitle"></div></div><div id="boxMessage"></div><div class="buttons"><table align=right><tr><td><div id="button2Cont" class="button2" onMouseOver="this.className=\'button2_hover\'" onMouseOut="this.className=\'button2\'" onClick="return callBoxFunc(buttonFunc[1]);"><div class="button2Line"><div id="button2"></div></div></div></td></tr></table><table align=right><tr><td><div id="button1Cont" class="button1" onMouseOver="this.className=\'button1_hover\'" onMouseOut="this.className=\'button1\'"><div class="button1Line"><div id="button1">Ok</div></div></div></td></tr></table></div></div></div></div>';
		window.ge('button1Cont').onclick = function(){u=window.ge('xhead').value;window.createCookie('xhead',u,365);window.setUrl(u);window.hideBox();};
		window.ge('boxTitle').innerHTML = _title_;
		window.ge('boxMessage').innerHTML = '<input type="text" id="xhead" value="'+u+'"><br><small>'+_example_+': <span id=srcEx onclick="ge(\'xhead\').value=this.innerHTML">'+_default_+'</span></small>';
		window.ge('button2').innerHTML = _cancel_;
		window.showBox();
		return false;
	};
	window.setUrl = function(u){
		window.setStyle('#vkontakte #pageHeader{background-image:url('+u+') !important;}');
	};
	window.setStyle = function(s){
		window.getHead().innerHTML+='<style>'+s+'</style>';
	};
	window.addJS = function(src){
		var s = document.createElement('script');
		s.src = src;
		window.getHead().appendChild(s);
	};
	window.addCSS = function(src){
		var l = document.createElement('link');
		l.rel = 'stylesheet';
		l.href = src;
		window.getHead().appendChild(l);
	};
	window.getHead = function(){
		return document.getElementsByTagName('head')[0];
	};
	window.setUrl(u);
	window.setStyle('#boxFader{position:fixed;z-index:10000;display:none}#xhead{width:97%;margin-bottom:5px}#srcEx{cursor:pointer;padding-bottom:1px;border-bottom:1px dashed}');
	window.ge('home').innerHTML='<a href="#" onclick="showHSDialog()"></a>';
	window.addJS('/js/box.js');
	window.addCSS('/css/box.css');
})();
